//index.js
var app = getApp();
// var WxParse = require('../../utils/wxParse/wxParse.js');
Page({
  data: {
    painting: {},
    shareImage: '',
    detail: '',
    userInfo: '',
    id: '',
    imgUrl:''
  },
  onLoad(options) {
    var _this = this;
    var id = options.id;
    //  var userinfomation = wx.getStorageSync('user_info')
    console.log(id)
    _this.setData({
      id: id,
    })
    var openid = wx.getStorageSync('user_new');
    if (!openid) {
      wx.showModal({
        title: '提示',
        content: '本功能需要授权后才可使用，点击确定跳转到登录界面',
        success(res) {
          if (res.confirm) {
            console.log('用户点击确定')
            setTimeout(function () {
              wx.switchTab({
                url: '/pages/member/index',
              });
            }, 500)
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
      return;
    }

    app.core('get', 'get_hot_news_detail/id/' + id + '/open_id/' + openid + '', {}, (res) => {
      console.log(res)
      _this.setData({
        detail: res.data
      })
      var url = res.data.photoUrl;
      var id = res.data.id;
      console.log(url)

      var imgUrl = url.replace('http','https')
      console.log(imgUrl)
      _this.setData({
        imgUrl: imgUrl
      })
      // app.core('post','operate_image',{
      //   type:2,
      //   id: id,
      //   url: url 
      // },((res) => {
      //   console.log(res.data)
      //   _this.setData({
      //     imgUrl: res.data
      //   })
      // }))
    })
    
    setTimeout(() => {
      _this.eventDraw()
    },1000)
  },
  getLocalTime(date) {
    // return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/, ' ');
    var date = new Date(date * 1000); //如果date为13位不需要乘1000
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
    var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
    var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
    var s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
    return Y + M + D + h + m + s;


  }, // 转换时间戳
  eventDraw() {
    wx.showLoading({
      title: '绘制海报中...',
      mask: true
    })
    var that = this;
    var detail = this.data.detail;
    console.log(detail.createTime)
    var ctime = that.getLocalTime(detail.createTime);
    console.log(ctime)
    // console.log(detail.content_article_news.content)
    // var detailContent = detail.content_article_news.content;
    // // var x = WxParse.wxParse('article', 'html', detailContent,that,5)
    // // console.log(x)
    // detailContent = detailContent.replace(/<[^>]+>/g, "").trim();  

    // detailContent = detailContent.replace(/[\'\"\\\/\b\f\n\r\t]/g, "").trim();

    // detailContent = detailContent.replace(/[\-\_\,\!\|\~\`\(\)\#\$\%\^\&\*\{\}\:\;\"\L\<\>\?]/g, "").trim();

    // detailContent = detailContent.substr(5)
  
    var userinfomation = wx.getStorageSync('userInfo');
    var imgUrl = this.data.imgUrl;
    console.log(imgUrl)
    this.setData({
      painting: {
        width: 375,
        height: 633,
        clear: true,
        views: [
          {
            type: 'image',
            url: 'https://xkcloude.hfhouse.com/storage/advert/2019-09-17/6uxrKJhTGSG21neAzkqmHJ97VqQKgD3WXeMHKwi3.png',
            top: 0,
            left: 0,
            width: 375,
            height: 633
          },
          {
            type: 'image',
            url: userinfomation.avatarUrl,
            top: 493,
            left: 35,
            width: 58,
            height: 58,
          },
          {
            type: 'text',
            content: userinfomation.nickName,
            fontSize: 13,
            color: '#034AB5',
            textAlign: 'left',
            top: 503,
            left: 100,
          },
          {
            type: 'text',
            content: '正在阅读这篇文章',
            fontSize: 13,
            color: '#034AB5',
            textAlign: 'left',
            top: 526,
            left: 100
          },
          {
            type: 'image',
            url: imgUrl ? imgUrl : 'https://xkcloude.hfhouse.com/storage/advert/2019-09-16/SyJEkYj6Qu9FZegW8xd0m1ZwtSbHcZHHUdXlyFGg.jpeg',
            top: 126,
            left: 19,
            width: 335,
            height: 186
          },
          {
            type: 'image',
            url: 'https://xkcloude.hfhouse.com/storage/advert/2019-09-09/fcMQKeB18axDBjQN1dMcta7ZOTBSvNzRvbC1Cnlg.png',
            top: 493,
            left: 250,
            width: 100,
            height: 100
          },
          {
            type: 'text',
            content: detail.title,
            fontSize: 21,
            lineHeight: 25,
            color: '#034AB5',
            textAlign: 'left',
            top: 32,
            left: 30,
            width: 280,
            MaxLineNumber: 2,
            breakWord: true,
            bolder: true
          },
          {
            type: 'text',
            content:'星房惠   '  +  ctime,
            fontSize: 14,
            color: '#2E2E2E',
            textAlign: 'left',
            top: 92,
            left: 30,
            width: 280,
          },
          {
            type: 'text',
            content: detail.description,
            fontSize: 16,
            lineHeight: 24,
            color: '#383549',
            textAlign: 'left',
            top: 338,
            left: 38,
            width: 287,
            MaxLineNumber: 5,
            breakWord: true,
          },
          // {
          //   type: 'text',
          //   content: '识别小程序二维码',
          //   fontSize: 13,
          //   color: '#2E65AC',
          //   textAlign: 'left',
          //   top: 555,
          //   left: 25,
          //   width: 125,
          // },
          {
            type: 'text',
            content: '识别二维码 进入星房惠获取更多新闻',
            fontSize: 13,
            color: '#2E65AC',
            textAlign: 'left',
            top: 580,
            left: 25,
            width: 125,
          }
        ]
      }
    })
  },
  eventSave() {
    var id = this.data.id;
    console.log(id)
    // console.log(this.data.shareImage)
    // let _this = this;
    wx.saveImageToPhotosAlbum({
      filePath: this.data.shareImage,
      success(res) {
        wx.showToast({
          title: '保存图片成功',
          icon: 'success',
          duration: 2000
        })
        setTimeout(function () {
          wx.switchTab({
            url: '/pages/information/index',
          });
        }, 1000)
      },
      fail(err) {
        if (err.errMsg === "saveImageToPhotosAlbum:fail:auth denied" || err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
          // 这边微信做过调整，必须要在按钮中触发，因此需要在弹框回调中进行调用
          wx.showModal({
            title: '提示',
            content: '需要您授权保存相册',
            showCancel: false,
            success: modalSuccess => {
              wx.openSetting({
                success(settingdata) {
                  console.log("settingdata", settingdata)
                  if (settingdata.authSetting['scope.writePhotosAlbum']) {
                    wx.showModal({
                      title: '提示',
                      content: '获取权限成功,再次点击图片即可保存',
                      showCancel: false,
                    })
                  } else {
                    wx.showModal({
                      title: '提示',
                      content: '获取权限失败，将无法保存到相册哦~',
                      showCancel: false,
                    })
                  }
                },
                fail(failData) {
                  console.log("failData", failData)
                },
                complete(finishData) {
                  console.log("finishData", finishData)
                }
              })
            }
          })
        }
      },
    })
  },
  eventGetImage(event) {
    console.log(event)
    wx.hideLoading()
    const { tempFilePath, errMsg } = event.detail
    if (errMsg === 'canvasdrawer:ok') {
      this.setData({
        shareImage: tempFilePath
      })
    }
  },
  previewImg(e) {
    var current = e.target.dataset.src;
    console.log(current)
    wx.previewImage({
      current: current,
      urls: [this.data.shareImage]
    })
  }
  // share() {
  //   wx.updateShareMenu({
  //     withShareTicket: true,
  //     success(res) {
  //       console.log(res)
  //     }
  //   })
  // }
})
